<?php
/**
 * Created by PhpStorm.
 * User: work
 * Date: 18-6-24
 * Time: 下午4:29
 */

namespace PddWechat\Models\User\Data;


use PddWechat\Library\InviteCodeLibrary;
use PddWechat\Models\User\Dao\UserDao;
use PddWechat\Models\User\Dao\UserPromotionUrlDao;
use PddWechat\Models\User\DataObject\UserDo;
use PddWechat\Models\User\DataObject\UserPromotionUrlDo;
use PddWechat\Models\User\Dto\WechatUserBasicInfo;

class UserData
{
    /**
     * User constructor.
     */
    public function __construct()
    {
        $this->userDao = new UserDao();
    }

    /**
     * @param string $openId
     * @return \PddWechat\Models\User\DataObject\UserDo
     */
    public function getUserByOpenId(string $openId){

        /** @var \PddWechat\Models\User\DataObject\UserDo $doUser */
        $doUser = $this->userDao->selectByOpenId($openId);
        unset($doUser->password);
        unset($doUser->slat);
        return $doUser;
    }


    /**
     * @param $inviteCode
     * @return UserDo
     */
    public function selectByInviteCode($inviteCode){
        /** @var \PddWechat\Models\User\DataObject\UserDo $doUser */
        $doUser = $this->userDao->selectByInviteCode($inviteCode);
        unset($doUser->password);
        unset($doUser->slat);
        return $doUser;
    }

    /**
     * @param array $openIds
     * @return UserDo
     */
    public function getUserByOpenIds(array $openIds){
        return $this->userDao->selectByOpenIds($openIds);
    }

    /**
     * @param WechatUserBasicInfo $basicUserInfo
     * @param $subscribe
     * @return UserDo
     */
    public function insert(WechatUserBasicInfo $basicUserInfo, $subscribe, $inviteOpenId="", $inviteUid=0){
        $userDo = new UserDo();
        $userDo->openId = $basicUserInfo->openId;
        $userDo->userName = $basicUserInfo->nickname;
        $userDo->province = $basicUserInfo->province;
        $userDo->avatar = $basicUserInfo->headimgurl;
        $userDo->city = $basicUserInfo->city;
        $userDo->unionId = $basicUserInfo->unionid;
        $userDo->sex = $basicUserInfo->sex;
        $userDo->subscribe = $subscribe;
        $userDo->inviteOpenId = $inviteOpenId;
        $userDo->inviteUid = $inviteUid;
        $userDo->inviteCode = InviteCodeLibrary::createCode();
        $userId =  $this->userDao -> insert($userDo);

        $userPromotionUrlDao = new UserPromotionUrlDao();
        $userPromotionUrlDo = new UserPromotionUrlDo();
        $userPromotionUrlDo->openId = $userDo->openId;
        $userPromotionUrlDo->mallUrl = \PddWechat\Library\PddLibrary::addMallUrl($userDo->openId, $userDo->inviteCode);
        $userPromotionUrlDo->redPackUrl = \PddWechat\Library\PddLibrary::addRedPackUrl($userDo->openId, $userDo->inviteCode);
        $userPromotionUrlDao->insert($userPromotionUrlDo);

        return $userId;
    }

    /**
     * @param $uid
     * @return \PddWechat\Models\User\DataObject\UserDo
     */
    public function selectByUid($uid)
    {
        /** @var \PddWechat\Models\User\DataObject\UserDo $doUser */
        $doUser =  $this->userDao -> select($uid);
        unset($doUser->password);
        return $doUser;
    }

    /**
     * @param $uids
     * @return array
     */
    public function selectByUids($uids)
    {
        $doUser =  $this->userDao -> selectByUids($uids);
        return $doUser;
    }

    public function total()
    {
        return  $this->userDao -> total();
    }


    /**
     * @param $pn
     * @param $rn
     * @return array
     */
    public function getList($pn, $rn){
        return $this->userDao->selectList($pn, $rn);
    }

    public function edit(\PddWechat\Models\User\DataObject\UserDo $userDo, $where=null){
        return $this->userDao->edit($userDo, $where);
    }

    public function getDirectMemberNumByOpenId(string $inviteopenid) {
        return $this->userDao->getDirectMemberNumByOpenId($inviteopenid);
    }

    public function getMemberListByOpenId(string $openId,$select = "*") {
        return $this->userDao->getMemberListByOpenId($openId,$select);
    }

    public function getIndirectMemberNumByOpenId(array $openIds) {
        return $this->userDao->getIndirectMemberNumByOpenId($openIds);
    }

    public function getMemberListAsPageListByOpenId(string $inviteopenid,$pn = 0,$rn = 0) {
        return $this->userDao->getMemberListAsPageListByOpenId($inviteopenid,$pn,$rn);
    }

    public function getDirectMemsIndirectMemsNumByOpenIds(array $openIds) {
        return $this->userDao->getDirectMemsIndirectMemsNumByOpenIds($openIds);
    }

    public function getDirectMemberNumByUidAndCreateTime(int $uid,$beginTime,$endTime) {
        return $this->userDao->getDirectMemberNumByUidAndCreateTime($uid,$beginTime,$endTime);
    }

    public function getDirectUsersByInviteUid(int $uid) {
        return $this->userDao->getDirectUsersByInviteUid($uid);
    }

    public function getIndirectUsersByInviteOpenId(array $openIds,$beginTime,$endTime) {
        return $this->userDao->getIndirectUsersByInviteOpenId($openIds,$beginTime,$endTime);
    }

    public function getUsersBuUids(array $uids) {
        return $this->userDao->getUsersBuUids($uids);
    }

    public function updateUserMoneyById(int $uid,$money) {
        return $this->userDao->updateUserMoneyById($uid,$money);
    }

    public function updateUserWithDrawMoney(int $uid,$money) {
        return $this->userDao->updateUserWithDrawMoney($uid, $money);
    }
}